//
//  settingList.swift
//  settingPageDemo
//
//  Created by hgc on 2021/6/11.
//

import SwiftUI

struct SettingList: View {
    @State var userList: [String] = UserPreferenceManager.getAvailableUsers()
    var usernameList: [String] {
        userList.map({UserPreferenceManager.manager(of: UUID(uuidString: $0)).getUsername()})
    }
    
    var callback: IndexCallback?
    var settingItemList = ["搜索引擎","首页","下载","清空浏览记录","YoruNeKo"]
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("账户"), footer: EmptyView()) {
                    ForEach(usernameList, id: \.self) { user in
                        NavigationLink(destination:
                            UserDetailSetting(uuidString: userList[usernameList.firstIndex(of: user) ?? 0])
                            .navigationTitle(user)) {
                            HStack {
                                Image(systemName: "person.crop.circle")
                                    .padding(.trailing)
                                Text(user)
                                Spacer()
                            }
                            
                        }
                    }
                    
                    NavigationLink(destination: AddUserSetting()){
                        HStack {
                            Image(systemName: "plus")
                                .padding(.trailing)
                            Text("新增用户配置")
                            Spacer()
                        }
                    }.foregroundColor(.blue)
                }
                .onAppear {
                    userList = UserPreferenceManager.getAvailableUsers()
                }
                
                Section(header: Text("基本") ) {
                    NavigationLink(destination: SearchEngineSetting().navigationTitle("搜索引擎")){
                        Text("搜索引擎")
                    }
                    NavigationLink(destination: HomepageSetting().navigationTitle("主页")){
                        Text("主页")
                    }
                    NavigationLink(destination: DownloadSetting().navigationTitle("下载")){
                        Text("下载")
                    }
                    NavigationLink(destination: HistorySetting().navigationTitle("无痕浏览")){
                        Text("无痕浏览")
                    }
                    NavigationLink(destination: AboutSetting().navigationTitle("关于")){
                        Text("关于 YoruNeko")
                    }
                }
                
            }
            .listStyle(PlainListStyle())
            .navigationBarTitleDisplayMode(.inline)
            .navigationTitle("设置")
            .navigationBarItems(
                leading:
                    Button {
                        onBackButtonPressed()
                    } label: {
                        HStack {
                            Image(systemName: "chevron.backward")
                            Text("返回")
                        }
                    })
//                trailing:
//                    Button {
//                        onSearchButtonPressed()
//                    } label: {
//                        Image(systemName: "magnifyingglass")
//                    })
            
        }
        
        
    }
    
    
    
    func onBackButtonPressed(){
        callback?.goBack()
    }
    func onSearchButtonPressed(){
        //callback?.goToView(by: .SearchView, sender: self, with: nil)
        callback?.goToView(to: .SearchView, from: .SettingView, intent: .Default, with: nil)
    }
}

struct settingList_Previews: PreviewProvider {
    static var previews: some View {
        SettingList()
    }
}
